package com.cskaoyan.jdbc.crud;

import com.cskaoyan.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 创建日期: 2022/11/16 16:20
 *
 * @author ciggar
 */
public class QueryDemo {

    public static void main(String[] args) throws SQLException {

        // 1. 获取连接
        Connection connection = JDBCUtils.getConnection();

        // 2. 获取statement对象
        Statement statement = connection.createStatement();

        // 3. 执行SQL语句
        ResultSet resultSet = statement.executeQuery("select id as sid,name,age from stu");

        // 4. 解析结果集

            // 向下移动  ,如果返回true，说明移动成功; 如果返回false，说明移动失败
            // resultSet.next();

            // 向上移动
            // resultSet.previous();

        while (resultSet.next()) {

            int sid = resultSet.getInt("sid");
            String name = resultSet.getString("name");
            int age = resultSet.getInt("age");

            System.out.println("sid:" + sid + ",name:" + name + ",age:" + age);
        }


        // 5. 关闭资源
        JDBCUtils.closeResources(resultSet,statement,connection);


    }
}
